package org.example.shop.service.login;

import jakarta.annotation.Resource;
import org.example.shop.entity.Admin;
import org.example.shop.entity.Users;
import org.example.shop.service.UserService;
import org.example.shop.service.admin.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PasswordStrategy implements LoginStrategy{
    @Resource
    private AdminService adminService;
    @Autowired
    private UserService userService;

    @Override
    public Users login(Object... params) {
        System.out.println("适配器模式：账号密码登录");
        if(params.length != 2){
            //参数小于两个，无法处理
            return null;
        }
        String account = (String)params[0];
        String password = (String)params[1];
        //是否管理员登录
        if(account.equals("admin")){
            //管理员登录
            Admin admin = adminService.login(account,password);
            if(admin!=null){
                Users u = new Users();
                u.setAccount(account);
                u.setPassword(password);
                return u;
            }
            return null;
        }else{
            //普通用户登录
            Users user = userService.login(account,password);
            if(user!=null){
                return user;
            }
            return null;
        }
    }

    @Override
    public String getLoginType() {
        return "password";
    }
}
